import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";

const initialState = 0;

export const incrementAsync = createAsyncThunk("counter/incrementAsync", async () => {
  const result = await new Promise<number>(resolve => {
    setTimeout(() => {
      resolve(3);
    }, 3000);
  });
  return result;
});
export const counterSlice = createSlice({
  name: "counter",
  initialState,
  reducers: {
    increment: state => state + 1,
    decrement: state => state - 1
  },
  extraReducers: builder => {
    builder.addCase(incrementAsync.fulfilled, (state, action) => state + action.payload);
  }
});

export const { increment, decrement } = counterSlice.actions;
export default counterSlice.reducer;
